package com.rookie.data.generate.util;


import com.rookie.data.generate.constant.ResponseResult;
import com.rookie.data.generate.constant.enums.ResultCodeEnum;

public class ResultUtils {
    /**
     * 返回一个returnData为空对象的成功消息的json
     *
     * @return 通用返回对象
     */
    public static <T> ResponseResult<T> success() {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResultCodeEnum.C_200.getCode());
        responseResult.setMessage(ResultCodeEnum.C_200.getMsg());
        return responseResult;
    }

    /**
     * 返回一个返回码为200的json
     *
     * @param returnData json里的主要内容
     * @return 通用返回对象
     */
    public static <T> ResponseResult<T> success(T returnData) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResultCodeEnum.C_200.getCode());
        responseResult.setMessage(ResultCodeEnum.C_200.getMsg());
        responseResult.setData(returnData);
        return responseResult;
    }


    /**
     * 返回错误信息JSON
     *
     * @param errorEnum 错误码的errorEnum
     * @return 通用返回对象
     */
    public static <T> ResponseResult<T> error(ResultCodeEnum errorEnum) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(errorEnum.getCode());
        responseResult.setMessage(errorEnum.getMsg());
        return responseResult;
    }

    public static <T> ResponseResult<T> error() {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResultCodeEnum.E_400.getCode());
        responseResult.setMessage(ResultCodeEnum.E_400.getMsg());
        return responseResult;
    }

    public static <T> ResponseResult<T> error(String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResultCodeEnum.E_503.getCode());
        responseResult.setMessage(message);
        return responseResult;
    }


    public static <T> ResponseResult<T> integerResult(int result) {
        if (result > 0) {
            return success();
        } else {
            return error();
        }
    }

    public static <T> ResponseResult<T> booleanResult(boolean result) {
        if (result) {
            return success();
        } else {
            return error(ResultCodeEnum.E_401);
        }
    }
}
